package com.hpch.business.controller;

import com.hpch.common.core.web.BaseController;
import com.hpch.business.service.ClassifyService;
import com.hpch.business.entity.Classify;
import com.hpch.business.param.ClassifyParam;
import com.hpch.common.core.web.ApiResult;
import com.hpch.common.core.web.PageResult;
import com.hpch.common.core.web.PageParam;
import com.hpch.common.core.web.BatchParam;
import com.hpch.common.core.annotation.OperationLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 网站资讯分类控制器
 *
 * @author EleAdmin
 * @since 2025-03-02 19:12:18
 */
@Api(tags = "网站资讯分类管理")
@RestController
@RequestMapping("/api/business/classify")
public class ClassifyController extends BaseController {
    @Resource
    private ClassifyService classifyService;

    @PreAuthorize("hasAuthority('business:product:list')")
    @OperationLog
    @ApiOperation("分页查询网站资讯分类")
    @GetMapping("/page")
    public ApiResult<PageResult<Classify>> page(ClassifyParam param) {
        PageParam<Classify, ClassifyParam> page = new PageParam<>(param);
        //page.setDefaultOrder("create_time desc");
//        return success(classifyService.page(page, page.getWrapper()));
        // 使用关联查询
        return success(classifyService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('business:product:list')")
    @OperationLog
    @ApiOperation("查询全部网站资讯分类")
    @GetMapping()
    public ApiResult<List<Classify>> list(ClassifyParam param) {
        PageParam<Classify, ClassifyParam> page = new PageParam<>(param);
        //page.setDefaultOrder("create_time desc");
//        return success(classifyService.list(page.getOrderWrapper()));
        // 使用关联查询
        return success(classifyService.listRel(param));
    }

    @PreAuthorize("hasAuthority('business:product:list')")
    @OperationLog
    @ApiOperation("根据id查询网站资讯分类")
    @GetMapping("/{id}")
    public ApiResult<Classify> get(@PathVariable("id") Integer id) {
//        return success(classifyService.getById(id));
        // 使用关联查询
        return success(classifyService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('business:product:save')")
    @OperationLog
    @ApiOperation("添加网站资讯分类")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Classify classify) {
        if (classifyService.save(classify)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('business:product:update')")
    @OperationLog
    @ApiOperation("修改网站资讯分类")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Classify classify) {
        if (classifyService.updateById(classify)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('business:product:remove')")
    @OperationLog
    @ApiOperation("删除网站资讯分类")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (classifyService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('business:product:save')")
    @OperationLog
    @ApiOperation("批量添加网站资讯分类")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Classify> list) {
        if (classifyService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('business:product:update')")
    @OperationLog
    @ApiOperation("批量修改网站资讯分类")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Classify> batchParam) {
        if (batchParam.update(classifyService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('business:product:remove')")
    @OperationLog
    @ApiOperation("批量删除网站资讯分类")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (classifyService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
